package cn.broadsense.thrid.api;

import cn.broadsense.common.exception.ServiceException;
import cn.broadsense.thrid.constant.DaHuaApiUrlConstant;
import cn.broadsense.thrid.param.*;
import cn.broadsense.thrid.result.*;
import cn.broadsense.thrid.utils.DaHuaHttpUtils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

/**
 * 车辆智能相关
 *
 * @author pengxin
 * @date 2022-10-13 16:20
 */
@Slf4j
@Component
public class VehicleAiRepository extends BaseRepository {

    /**
     * 过车查询
     *
     * @param param
     * @return
     */
    public VehicleQueryListResult vehicleQuery(VehicleQueryListParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_QUERY_LIST, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 过车记录详情
     *
     * @param recordId 记录id
     * @return
     */
    public VehicleQueryDetailResult vehicleQueryDetail(String recordId) {
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return VehicleQueryDetailResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_QUERY_DETAILS, recordId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }


    /**
     * 违章查询
     *
     * @param param
     * @return
     */
    public BasePageResult<VehicleIllegalResult> vehicleIllegal(VehicleIllegalParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_ILLEGAL_LIST, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 违章详情
     *
     * @param recordId
     * @return
     */
    public BasePageResult<VehicleIllegalResult> vehicleIllegalDetail(String recordId) {
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return BasePageResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_ILLEGAL_DETAILS, recordId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 新增布控
     *
     * @param param
     * @return
     */
    public String surveillanceAdd(VehicleSurveillanceAddParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.VEHICLE_SURVEILLANCE);
        JSONObject object = DaHuaHttpUtils.postRest(getCToken(), param);
        if (object.containsKey("recordId")) {
            return object.getString("recordId");
        }
        log.error("surveillanceAdd 新增失败:{}", object);
        throw new ServiceException("新增布控失败");
    }

    /**
     * 布控审核
     *
     * @param param
     */
    public void surveillanceAudit(VehicleSurveillanceAuditParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_SURVEILLANCE_AUDIT, param.getRecordId()));
        DaHuaHttpUtils.putRest(getCToken(), param);
    }

    /**
     * 布控撤销
     *
     * @param param
     */
    public void surveillanceRevoke(VehicleSurveillanceRevokeParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_SURVEILLANCE_REVOKE, param.getRecordId()));
        DaHuaHttpUtils.putRest(getCToken(), param);
    }

    /**
     * 布控列表查询
     *
     * @param param
     * @return
     */
    public BasePageResult<VehicleSurveillanceDetailResult> surveillanceList(VehicleSurveillanceParam param) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(param, new TreeMap<>(), false, true);
        stringObjectMap.put("resultClass",null);
        stringObjectMap.put("ignoreList",null);
        stringObjectMap.put("url",null);
        String query = MapUtil.joinIgnoreNull(stringObjectMap, "&", "=");
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.VEHICLE_SURVEILLANCE_LIST + "?" + query);
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 查询布控详情
     *
     * @param recordId
     * @return
     */
    public VehicleSurveillanceDetailResult surveillanceDetail(String recordId) {
        BaseParam param = new BasePageParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return VehicleSurveillanceDetailResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_SURVEILLANCE_DETAIL, recordId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }


    /**
     * 告警列表查询
     *
     * @param param
     * @return
     */
    public BasePageResult<VehicleAlarmResult> vehicleAlarmList(VehicleAlarmParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_ALARM,param.getPage(),param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 告警详细信息
     *
     * @param recordId
     * @return
     */
    public VehicleAlarmResult vehicleAlarmDetail(String recordId) {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return VehicleAlarmResult.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.VEHICLE_ALARM_DETAIL, recordId));
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 二次分析
     *
     * @param param
     * @return
     */
    public BasePageResult<VehicleAnalyseResult> analyse(VehicleAnalyseParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.PAS_CAR_ANALYSE, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 车辆品牌
     *
     * @return
     */
    public VehicleCarSeriesResult carSeries() {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return VehicleCarSeriesResult.class;
            }
        };
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CAR_SERIES);
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }

    /**
     * 搜索图片中的车辆
     *
     * @param param
     * @return
     */
    public VehiclePicQueryResult picQuery(VehiclePicQueryParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.PIC_QUERY);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 添加以图搜图任务
     *
     * @param param
     * @return
     */
    public VehiclePicTaskResult picTask(VehiclePicTaskParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.PIC_TASK);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    public BigDecimal picTaskStatus(String taskId) {
        BaseParam param = new BaseParam() {
            @Override
            public void checkParam() {

            }

            @Override
            public Class getResultClass() {
                return JSONObject.class;
            }
        };
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.PIC_TASK_STATUS, taskId));
        JSONObject object = DaHuaHttpUtils.getRest(getCToken(), param);
        if (object.containsKey("data")) {
            return object.getBigDecimal("data");
        }
        log.error("查询以图搜图任务失败：{}", object);
        throw new ServiceException("查询以图搜图任务失败");
    }

    /**
     * 获取异步以图搜图任务结果，返回相似度
     *
     * @param param
     * @return
     */
    public VehiclePicTaskDetailsResult picTaskDetails(VehiclePicTaskDetailParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.PIC_TASK_RESULT, param.getTaskId(), param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 过车总量统计
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult carFlow(VehicleCarFlowParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CAR_FLOW);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对过车总量按照通道统计查询。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult channelFlow(VehicleChannelFlowParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CHANNEL_FLOW);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对违章报警按照地点纬度统计查询。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult violationChannel(VehicleChannelParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.VIOLATION_CHANNEL);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对违章报警按照时间纬度统计查询。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult violationCount(VehicleCountParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.VIOLATION_COUNT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 统 ——违法统计车辆类型接口
     * 根据条件对违法数据的车辆类型进行统计。 当前版本是直接调用的云数据库通用接口查询得到的数据。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult violationType(ViolationPicReportParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.VIOLATION_PLATE_TYPE);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 统 ——违法统计车辆类型接口
     * 根据条件对违法数据的违章类型进行统计。 当前版本是直接调用的云数据库通用接口查询得到的数据。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult violationRecType(ViolationPicReportParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.VIOLATION_REC_TYPE);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对过车布控报警按照地点纬度统计查询。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult surveyAlarmChannel(VehicleReportAlarmParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.SURVEY_ALARM_CHANNEL);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对过车布控报警按照时间纬度统计查询。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult surveyAlarmCount(VehicleReportAlarmParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.SURVEY_ALARM_COUNT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对过车总量按照车辆类别统计查询。
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult categoryCount(VehicleReportCategoryParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CATEGORY_COUNT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 针对过车总量按照车系统计查询
     *
     * @param param
     * @return
     */
    public VehicleStatisticsResult carBrandCount(VehicleReportBrandParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CAR_BRAND_COUNT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 在所选时间范围内，且上次出现的时间在回溯天数之前（或从来没出现过）的车辆（查过车表）
     *
     * @param param
     * @return
     */
    public VehicleFirstAppearResult firstAppear(VehicleFirstAppearParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.FIRST_APPEAR_LIST);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 在所选时间范围内，且上次出现的时间在回溯天数之前（或从来没出现过）的车辆（查过车表）的总数
     *
     * @param param
     * @return
     */
    public VehicleAppearCountResult firstAppearCount(VehicleAppearCountParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.FIRST_APPEAR_COUNT);
        return DaHuaHttpUtils.getRest(getCToken(), param);
    }


    /**
     * 时空碰撞 汇总查询
     * 在查询时间段内（可多个时间段），多个地点同一车辆出现的命中数
     *
     * @param param
     * @return
     */
    public VehicleTrackCollisionResult trackCollision(VehicleTrackCollisionParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.TRACK_COLLISION_LIST, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 在查询时间段内（可多个时间段），多个地点同一车辆出现的命中数总数。
     *
     * @param param
     * @return
     */
    public VehicleAppearCountResult trackCollisionCount(VehicleTrackCollisionParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.TRACK_COLLISION_COUNT);
        param.setResultClass(VehicleAppearCountResult.class);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 在查询时间段内（可多个时间段），多个地点同一车辆过车详情。
     *
     * @param param
     * @return
     */
    public VehicleTackDetailResult trackCollisionDetail(VehicleTrackCollisionParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.TRACK_COLLISION_DETAIL, param.getPage(), param.getPageSize()));
        param.setResultClass(VehicleTackDetailResult.class);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 在查询时间段内（可多个时间段），多个地点同一车辆过车详情命中数总数。
     *
     * @param param
     * @return
     */
    public VehicleAppearCountResult trackCollisionDetailCount(VehicleTrackCollisionParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.TRACK_COLLISION_DETAIL_COUNT);
        param.setResultClass(VehicleAppearCountResult.class);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 根据查询条件查询同行车信息。
     *
     * @param param
     * @return
     */
    public VehicleFollowCarResult followCar(VehicleFollowCarParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.FOLLOW_CAR, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 车辆轨迹
     *
     * @param param
     * @return
     */
    public VehicleCarTracesResult carTraces(VehicleCarTracesParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CAR_TRACES);
        return DaHuaHttpUtils.postRest(getCToken(), param);

    }

    /**
     * 车辆轨迹详情
     *
     * @param param
     * @return
     */
    public VehicleTracesDetailResult carTracesDetail(VehicleTracesDetailParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.CAR_TRACES_DETAIL);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 频繁出现记录
     *
     * @param param
     * @return
     */
    public VehicleHighFrequencyResult highFrequency(VehicleHighFrequencyParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.HIGH_FREQUENCY_CAR, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 频繁出现总数
     *
     * @param param
     * @return
     */
    public Integer highFrequencyCount(VehicleHighFrequencyParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.HIGH_FREQUENCY_CAR_COUNT);
        VehicleHighFrequencyResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 落脚点分析记录
     *
     * @param param
     * @return
     */
    public VehicleLocationResult locationAnalyse(VehicleLocationParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.LOCATION_ANALYSE, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 落脚点分析总数
     *
     * @param param
     * @return
     */
    public Integer locationAnalyseCount(VehicleLocationParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.LOCATION_ANALYSE_COUNT);
        VehicleLocationResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 落脚点详情记录 根据点位查询
     *
     * @param param
     * @return
     */
    public VehicleLocationResult locationDetail(VehicleLocationDetailParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.LOCATION_ANALYSE_DETAIL, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 落脚点详情总数
     *
     * @param param
     * @return
     */
    public Integer locationDetailCount(VehicleLocationParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.LOCATION_ANALYSE_DETAIL_COUNT);
        VehicleLocationResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 隐匿车辆查询接口
     * 查询在指定时间范围内出现，而之后的隐匿天数内不出现的车辆信息
     *
     * @param param
     * @return
     */
    public VehicleHideCarResult hideCar(VehicleHideCarParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.HIDE_CAR, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 隐匿车辆总数
     * 查询在指定时间范围内出现，而之后的隐匿天数内不出现的车辆总数。
     *
     * @param param
     * @return
     */
    public Integer hideCarCount(VehicleLocationParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.HIDE_CAR_COUNT);
        param.setResultClass(VehicleLocationResult.class);
        VehicleLocationResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 昼伏夜出 查询
     * 在特定时间满足夜间出现次数和白天出现次数的车辆。
     *
     * @param param
     * @return
     */
    public VehicleDhnoResult dhno(VehicleDhnoParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.DHNO, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 昼伏夜出 查询总数
     * 导出在特定时间满足夜间出现次数和白天出现次数的车辆。
     *
     * @param param
     * @return
     */
    public Integer dhnoCount(VehicleDhnoParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.DHNO_COUNT);
        param.setResultClass(VehicleDhnoResult.class);
        VehicleDhnoResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 套牌车 查询
     * 根据查询条件，查询研判出的套牌车数据。
     *
     * @param param
     * @return
     */
    public VehicleCopyCarResult copyCar(VehicleCopyCarParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.COPY_CAR, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 套牌车 查询总数
     *
     * @param param
     * @return
     */
    public Integer copyCarCount(VehicleCopyCarParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.COPY_CAR_COUNT);
        param.setResultClass(VehicleCopyCarResult.class);
        VehicleCopyCarResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 根据ID，查询研判出的套牌车数据。
     *
     * @param param
     * @return
     */
    public VehicleCopyCarResult copyCarDetail(VehicleCopyCarDetailParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.COPY_CAR_DETAIL);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }


    /**
     * 套牌车 查询
     * 根据查询条件，查询研判出的套牌车数据。
     *
     * @param param
     * @return
     */
    public VehicleFakeCarResult fakeCar(VehicleFakeCarParam param) {
        param.setUrl(cUrl + String.format(DaHuaApiUrlConstant.VehicleAi.FAKE_CAR, param.getPage(), param.getPageSize()));
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 套牌车 查询总数
     *
     * @param param
     * @return
     */
    public Integer fakeCarCount(VehicleCopyCarParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.FAKE_CAR_COUNT);
        param.setResultClass(VehicleFakeCarResult.class);
        VehicleFakeCarResult result = DaHuaHttpUtils.postRest(getCToken(), param);
        return result.getTotalCount();
    }

    /**
     * 根据ID，查询研判出的套牌车数据。
     *
     * @param param
     * @return
     */
    public VehicleFakeCarResult fakeCarDetail(VehicleCopyCarDetailParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VehicleAi.FAKE_CAR_DETAIL);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

}
